package lib

import (
	"GameServer/Common/utils"
	"fmt"

	"GameServer/Common/data"
	"GameServer/Game/errors"
	"GameServer/Game/m"
)

// 给日常广告奖励
func GiveAdDailyRewards(from string, u *m.User, PSend func(interface{}) error) (rAdReward *m.AdReward, uProp *m.UpdateProp, resGoodsList []*m.Goods, boxResList []*m.BoxRes, err error) {
	adRewards := u.GetAdRewards()        //获取该玩家的广告记录
	adReward := adRewards.GetAdReward(1) //获取typ == 1，日常广告奖励
	watchNum := adReward.WatchNum + 1
	/*
		tpl, ok := data.Get("AdDailyRewards", uint64(watchNum)) //获取第watchNum次奖励
		if !ok {
			err = errors.NewErrcode(data.Errcode.ConfigNotFound, fmt.Sprintf("没有找到广告体力奖励,AdDailyRewards,id:%d", watchNum))
			return
		}
		adRewardT := tpl.(data.AdDailyReward)

		giveGoods := make([]m.BaseKV, 0)

		for _, item := range adRewardT.Rewards {
			giveGoods = append(giveGoods, m.BaseKV{Key: int64(item.Key), Val: int64(item.Value)})
		}*/
	//根据玩家钻石判断
	giveGoods := make([]m.BaseKV, 0)
	if u.Diamon >= 50 {
		//钻石大于等于50，走配置1
		tpl, ok := data.GetAllDatas("AdTurnTableReward1")
		if !ok {
			err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到日常广告奖励,AdTurnTableReward1")
			return
		}
		var weightKvs []data.WeightKV
		for _, value := range tpl {
			adRewardT := value.(data.AdTurnTableReward).Rewards
			weightKvs = append(weightKvs, adRewardT)
		}
		aaaa := utils.RandomWeightKV(weightKvs)
		giveGoods = append(giveGoods, m.BaseKV{Key: int64(aaaa.Key), Val: int64(aaaa.Value)})
	} else {
		//钻石小于50，走配置2
		tpl, ok := data.GetAllDatas("AdTurnTableReward2")
		if !ok {
			err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到日常广告奖励,AdTurnTableReward1")
			return
		}
		var weightKvs []data.WeightKV
		for _, value := range tpl {
			adRewardT := value.(data.AdTurnTableReward).Rewards
			weightKvs = append(weightKvs, adRewardT)
		}
		aaaa := utils.RandomWeightKV(weightKvs)
		giveGoods = append(giveGoods, m.BaseKV{Key: int64(aaaa.Key), Val: int64(aaaa.Value)})
	}
	uProp, resGoodsList, boxResList, err = GiveGoodsByKvList(from, u, giveGoods, PSend)
	if err != nil {
		return
	}

	adRewards.UpdateAdReward(1, watchNum)
	rAdReward = &m.AdReward{Time: utils.TNow().Unix(), Typ: 1, WatchNum: watchNum}
	return
}

// 给广告 体力奖励
func GiveAdEnergyRewards(from string, u *m.User, PSend func(interface{}) error) (rAdReward *m.AdReward, uProp *m.UpdateProp, resGoodsList []*m.Goods, boxResList []*m.BoxRes, err error) {
	adRewards := u.GetAdRewards()
	adReward := adRewards.GetAdReward(2)
	watchNum := adReward.WatchNum + 1
	tpl, ok := data.Get("AdEnergyRewards", uint64(watchNum))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, fmt.Sprintf("没有找到广告体力奖励,AdEnergyRewards,id:%d", watchNum))
		return
	}
	adRewardT := tpl.(data.AdEnergyReward)

	giveGoods := make([]m.BaseKV, 0)

	for _, item := range adRewardT.Rewards {
		giveGoods = append(giveGoods, m.BaseKV{Key: int64(item.Key), Val: int64(item.Value)})
	}

	uProp, resGoodsList, boxResList, err = GiveGoodsByKvList(from, u, giveGoods, PSend)
	if err != nil {
		return
	}

	adRewards.UpdateAdReward(2, watchNum)
	rAdReward = &m.AdReward{Time: utils.TNow().Unix(), Typ: 2, WatchNum: watchNum}
	return
}

// 给广告 金币奖励
func GiveAdGoldRewards(from string, u *m.User, PSend func(interface{}) error) (rAdReward *m.AdReward, uProp *m.UpdateProp, resGoodsList []*m.Goods, boxResList []*m.BoxRes, err error) {
	adRewards := u.GetAdRewards()
	adReward := adRewards.GetAdReward(3)
	watchNum := adReward.WatchNum + 1
	tpl, ok := data.Get("AdGoldRewards", uint64(watchNum))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, fmt.Sprintf("没有找到广告金币奖励,AdGoldRewards,id:%d", watchNum))
		return
	}
	adRewardT := tpl.(data.AdGoldReward)

	giveGoods := make([]m.BaseKV, 0)

	for _, item := range adRewardT.Rewards {
		if int64(item.Id) == u.MaxPos.LockBlockTid {
			giveGoods = append(giveGoods, m.BaseKV{Key: int64(item.Key), Val: int64(item.Value)})
			break
		}

	}

	uProp, resGoodsList, boxResList, err = GiveGoodsByKvList(from, u, giveGoods, PSend)
	if err != nil {
		return
	}

	adRewards.UpdateAdReward(3, watchNum)
	rAdReward = &m.AdReward{Time: utils.TNow().Unix(), Typ: 3, WatchNum: watchNum}
	return
}

// 给祭坛广告奖励
func GiveAdAltarRewards(u *m.User) (rAdReward *m.AdReward, err error) {
	adRewards := u.GetAdRewards()
	adReward := adRewards.GetAdReward(4)
	watchNum := adReward.WatchNum + 1
	tpl, ok := data.Get("AdAltarRewards", uint64(1))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, fmt.Sprintf("没有找到祭坛奖励,AdAltarRewards,id:%d", 1))
		return
	}
	adRewardT := tpl.(data.AdAltarReward)

	if watchNum > int64(adRewardT.Num) {
		err = errors.NewErrcode(data.Errcode.ServerErr, fmt.Sprintf("祭坛广告奖励没有观看次数,watchNum:%d", watchNum))
		return
	}

	adRewards.UpdateAdRewardAltarNum(watchNum, 1)
	rAdReward = &m.AdReward{Time: utils.TNow().Unix(), Typ: 4, WatchNum: watchNum}
	return
}
